package controller

import (
	"github.com/gin-gonic/gin"
	"go-demo/common"
	"go-demo/model"
	"go-demo/service"
	"net/http"
	"strconv"
)

// token string 用户鉴权token 必需
// video_id string 视频id 必需
// action_type string 必需 1-点赞，2-取消点赞
// 登录用户对视频的点赞和取消点赞操作
func FavoriteAction(c *gin.Context) {
	token := c.Query("token")
	videoId, _ := strconv.ParseInt(c.Query("video_id"), 10, 64)
	actionType := c.Query("action_type")
	//验证是否登录，即是否存在token
	//可以查到用户的id
	if user, exist := common.UsersLoginInfo[token]; exist {
		service.FavoriteAction(actionType, user, videoId)
		c.JSON(http.StatusOK, model.Response{StatusCode: common.SUCCESS, StatusMsg: "操作成功"})
	} else {
		c.JSON(http.StatusOK, model.Response{StatusCode: common.ERROR, StatusMsg: "操作失败"})
	}

}

// user_id string 用户id 必需
// token string 用户鉴权token 必需
func FavoriteList(c *gin.Context) {
	userId, _ := strconv.ParseInt(c.Query("user_id"), 10, 64)
	token := c.Query("token")
	if _, exist := common.UsersLoginInfo[token]; exist {
		videoList := service.FavoriteList(userId)
		c.JSON(http.StatusOK, VideoListResponse{
			Response: model.Response{
				StatusCode: common.SUCCESS,
				StatusMsg:  "操作成功",
			},
			VideoList: videoList,
		})
	} else {
		c.JSON(http.StatusOK, model.Response{StatusCode: common.ERROR, StatusMsg: "操作失败"})
	}
}
